<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="120px">
			<el-form-item label="移位任务编号" prop="boxMoveNo">
				<el-input v-model="queryParams.boxMoveNo" placeholder="请输入移位任务编号" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="移位状态" prop="boxMoveStatus">
				<el-select v-model="queryParams.boxMoveStatus" placeholder="请选择移位状态" clearable>
					<el-option v-for="dict in shift_status" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<!--      <el-col :span="1.5">-->
			<!--        <el-button-->
			<!--          type="primary"-->
			<!--          plain-->
			<!--          icon="Plus"-->
			<!--          @click="handleAdd"-->
			<!--          v-hasPermi="['archive:ArchiveBoxMoveTask:add']"-->
			<!--        >新增</el-button>-->
			<!--      </el-col>-->
			<!--      <el-col :span="1.5">-->
			<!--        <el-button-->
			<!--          type="success"-->
			<!--          plain-->
			<!--          icon="Edit"-->
			<!--          :disabled="single"-->
			<!--          @click="handleUpdate"-->
			<!--          v-hasPermi="['archive:ArchiveBoxMoveTask:edit']"-->
			<!--        >修改</el-button>-->
			<!--      </el-col>-->
			<!--      <el-col :span="1.5">-->
			<!--        <el-button-->
			<!--          type="danger"-->
			<!--          plain-->
			<!--          icon="Delete"-->
			<!--          :disabled="multiple"-->
			<!--          @click="handleDelete"-->
			<!--          v-hasPermi="['archive:ArchiveBoxMoveTask:remove']"-->
			<!--        >删除</el-button>-->
			<!--      </el-col>-->
			<!-- <el-col :span="1.5">
				<el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['archive:ArchiveBoxMoveTask:export']">导出</el-button>
			</el-col> -->
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="ArchiveBoxMoveTaskList" @selection-change="handleSelectionChange">
			<!-- <el-table-column type="selection" width="55" align="center" /> -->
			<el-table-column label="序号" align='center' width="50">
				<template #default="scope">
					{{ scope.$index+1 }}
				</template>
			</el-table-column>
			<!--      <el-table-column label="序号" align="center" prop="taskId" />-->
			<el-table-column label="移位任务编号" align="center" prop="boxMoveNo" />
			<!-- <el-table-column label="档案室编号" align="center" prop="roomNo" /> -->

			<el-table-column label="移位状态" align="center" prop="boxMoveStatus">
				<template #default="scope">
					<dict-tag :options="shift_status" :value="scope.row.boxMoveStatus" />
				</template>
			</el-table-column>
			<el-table-column label="操作人" align="center" prop="modifier" width="100">
			</el-table-column>
			<el-table-column label="操作时间" align="center" prop="modifyTime" width="160">
				<template #default="scope">
					<span>{{ parseTime(scope.row.modifyTime) }}</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" fixed="right" width="280">
				<template #default="scope">
					<el-button type="text" icon="View" @click="handleView(scope.row, scope.index)" v-hasPermi="['archive:ArchiveBoxMoveTask:query']">详细</el-button>
					&nbsp; <el-button v-if="scope.row.boxMoveStatus == '01'" type="text" icon="Upload" @click="submitStatus(scope.row, scope.index)"
					 v-hasPermi="['archive:ArchiveBoxMoveTask:edit']">移位完成</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
		 @pagination="getList" />

		<!-- 查看详情 -->
		<el-dialog title="档案盒移位详细" v-model="openscan" width="90%" append-to-body>
			<el-form ref="ArchiveBoxMoveTaskRef" :model="form" label-width="170px">
				<el-row>
					<el-col :span="6">
						<el-form-item label="移位任务编号" prop="boxMoveNo">
							<el-input v-model="form.boxMoveNo" style="width:230px;" disabled="disabled" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="移位状态" prop="boxMoveStatus">
							<el-select v-model="form.boxMoveStatus" style="width:230px;" disabled="disabled">
								<el-option v-for="dict in shift_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="操作人" prop="modifier">
							<el-input v-model="form.modifier" style="width:230px;" disabled="disabled" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="操作时间" prop="modifyTime">
							<el-input v-model="form.modifyTime" style="width:230px;" disabled="disabled" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-divider content-position="center" v-if="form.boxMoveStatus=='02'">移位明细数据信息</el-divider>
				<el-row :gutter="10" class="mb8" v-if="form.boxMoveStatus!='02'">
					<!-- <template #default="scope">
						<el-col :span="3">
							<el-input v-model="inputkwh" placeholder="请输入新库位号,统一设置" style="width:190px;" maxlength="20" @input="KWChange"
							 @focus="KWFocus"></el-input>
						</el-col>
						<el-col :span="1.5">
							<el-button type="primary" icon="CircleCheck" @click="handleSave()" v-hasPermi="['archive:ArchiveBoxMoveTask:edit']">保存新库位</el-button>
						</el-col>
					</template> -->
				</el-row>
				<el-row>
					<el-table :data="archiveMoveList" ref="archiveBoxMove" style="min-height: 550px;">
						<el-table-column label="序号" align='center' width="50">
							<template #default="scope">
								{{ scope.$index+1 }}
							</template>
						</el-table-column>
						<el-table-column label="档案盒号" prop="boxNo" align="center">
						</el-table-column>
						<el-table-column label="新库位号" prop="newHouseLocationNo" align="center">
							<!-- <template #default="scope">
								<el-input v-model="scope.row.newHouseLocationNo" style="width:130px;" maxlength="20"></el-input>
							</template> -->
						</el-table-column>
						
					</el-table>
				</el-row>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button @click="openscan = false">关 闭</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="ArchiveBoxMoveTask">
	import {
		listArchiveBoxMoveTask,
		getArchiveBoxMoveTask,
		delArchiveBoxMoveTask,
		addArchiveBoxMoveTask,
		updateArchiveBoxMoveTask,
		selectArchiveBoxMoveByTaskId,
		editarchiveBoxMoveList,
		endArchiveBoxMoveList,
		archiveBoxMoveTaskResultReport
	} from "@/api/archive/ArchiveBoxMoveTask";
	import {
		getCurrentInstance,
		reactive,
		ref,
		toRefs
	} from "vue";
	import {
		getStorageTask,
		updateStorageTask
	} from "@/api/archive/storageTask";
	const {
		proxy
	} = getCurrentInstance();

	const ArchiveBoxMoveTaskList = ref([]);
	const open = ref(false);
	const loading = ref(true);
	const showSearch = ref(true);
	const ids = ref([]);
	const single = ref(true);
	const multiple = ref(true);
	const total = ref(0);
	const title = ref("");
	const dateRange = ref([]);
	const openscan = ref(false);
	const archiveMoveList = ref([]);
	const data = reactive({
		form: {},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			boxMoveNo: null,
			roomNo: null,
			pushStatus: null,
			pushTime: null,
			status: null,
			createTime: null,
			boxMoveStatus: null
		},
		rules: {}
	});
	let inputkwh = ref('');
	const {
		queryParams,
		form,
		rules
	} = toRefs(data);
	const {
		push_status,
		execut_result,
		shift_status
	} = proxy.useDict('push_status', 'execut_result', 'shift_status');

	/** 查询盒移位任务列表 */
	function getList() {
		loading.value = true;
		listArchiveBoxMoveTask(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
			//listArchiveBoxMoveTask(queryParams.value).then(response => {
			ArchiveBoxMoveTaskList.value = response.rows;
			total.value = response.total;
			loading.value = false;
		});
	}

	// 取消按钮
	function cancel() {
		open.value = false;
		reset();
	}

	// 表单重置
	function reset() {
		form.value = {
			taskId: null,
			boxMoveNo: null,
			orgNo: null,
			roomNo: null,
			pushStatus: "0",
			pushTime: null,
			status: "0",
			reportTime: null,
			createTime: null,
			creator: null,
			modifyTime: null,
			modifier: null,
			boxMoveStatus: "0"
		};
		proxy.resetForm("ArchiveBoxMoveTaskRef");
		archiveMoveList.value = [];
		inputkwh.value = '';
	}

	/** 搜索按钮操作 */
	function handleQuery() {
		queryParams.value.pageNum = 1;
		getList();
	}

	/** 重置按钮操作 */
	function resetQuery() {
		dateRange.value = [];
		proxy.resetForm("queryRef");
		handleQuery();
	}

	// 多选框选中数据
	function handleSelectionChange(selection) {
		ids.value = selection.map(item => item.taskId);
		single.value = selection.length != 1;
		multiple.value = !selection.length;
	}

	/** 新增按钮操作 */
	function handleAdd() {
		reset();
		open.value = true;
		title.value = "添加盒移位任务";
	}

	/** 修改按钮操作 */
	function handleUpdate(row) {
		reset();
		const taskId = row.taskId || ids.value
		getArchiveBoxMoveTask(taskId).then(response => {
			form.value = response.data;
			open.value = true;
			title.value = "修改盒移位任务";
		});
	}

	/** 提交按钮 */
	function submitForm() {
		proxy.$refs["ArchiveBoxMoveTaskRef"].validate(valid => {
			if (valid) {
				if (form.value.taskId != null) {
					updateArchiveBoxMoveTask(form.value).then(response => {
						proxy.$modal.msgSuccess("修改成功");
						open.value = false;
						getList();
					});
				} else {
					addArchiveBoxMoveTask(form.value).then(response => {
						proxy.$modal.msgSuccess("新增成功");
						open.value = false;
						getList();
					});
				}
			}
		});
	}

	/** 删除按钮操作 */
	function handleDelete(row) {
		const taskIds = row.taskId || ids.value;
		proxy.$modal.confirm('是否确认删除盒移位任务编号为"' + taskIds + '"的数据项？').then(function() {
			return delArchiveBoxMoveTask(taskIds);
		}).then(() => {
			getList();
			proxy.$modal.msgSuccess("删除成功");
		}).catch(() => {});
	}

	/** 导出按钮操作 */
	function handleExport() {
		proxy.download('archive/ArchiveBoxMoveTask/export', {
			...queryParams.value
		}, `档案盒移位_${new Date().getTime()}.xlsx`)
	}

	function handleView(row) {
		openscan.value = true;
		inputkwh.value = '';
		const taskId = row.taskId;
		getArchiveBoxMoveTask(taskId).then(response => {
			form.value = response.data;
		});
		selectArchiveBoxMoveByTaskId(taskId).then(response => {
			archiveMoveList.value = response.data;
		});
	}

	function submitReport(row) {
		const boxMoveNo = row.boxMoveNo;

		if (row.boxMoveStatus == "01") {
			proxy.$modal.msgError('移位状态为【待移位】，上报失败！');
		} else if (row.boxMoveStatus === "02") {
			form.value.boxMoveNo = boxMoveNo;
			archiveBoxMoveTaskResultReport(form.value).then(response => {
				proxy.$modal.msgSuccess("移位信息上报成功");
				getList();
			}).catch(error => {
				console.error("移位信息上报失败:", error);
			});
		}
	}

	function handleSave(row) {
		// archiveMoveList.value.forEach((item, index)=> {
		//   alert(item.newHouseLocationNo); //输出数组的每一个元素
		// });
		editarchiveBoxMoveList(archiveMoveList.value).then(response => {
			// {"msg":"操作成功","code":200}
			if (response.code == "200") {
				proxy.$modal.msgSuccess('新库位保存成功！');
				getList();
				openscan.value = false;
			}
		});
	}

	function KWChange(e) {
		inputkwh.value = e;
		archiveMoveList.value.forEach((item, index) => {
			item.newHouseLocationNo = inputkwh.value;
		});
	}

	function KWFocus(e) {
		archiveMoveList.value.forEach((item, index) => {
			item.newHouseLocationNo = '';
		});
	}

	function submitStatus(row) {
		const taskId = row.taskId;
		if (row.boxMoveStatus == "02") {
			proxy.$modal.msgError('单据状态为【移位完成】，不需要重复点击！');
		} else {
			selectArchiveBoxMoveByTaskId(taskId).then(response => {
				endArchiveBoxMoveList(response.data).then(response => {
					// {"msg":"操作成功","code":200}
					if (response.code == "500") {
						proxy.$modal.msgError(response.msg);
					} else if (response.code = "200") {
						proxy.$modal.msgSuccess("移位完成！");
						getList();
					}
				});
			});
		}
	}
	getList();
</script>
